package cn.edu.gzmtu.controller;

import cn.edu.gzmtu.domain.Product;
import cn.edu.gzmtu.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private IProductService productService;

    /**
     * 查询所有产品
     * @return
     * @throws Exception
     */
    @RequestMapping("/findAll.do")
    public ModelAndView findAll() throws Exception {

        ModelAndView mv = new ModelAndView();

        List<Product> productList = productService.findAll();
        mv.addObject("productList", productList);

        mv.setViewName("product-list");

        return mv;
    }

    /**
     * 添加产品
     * @return
     * @throws Exception
     */
    @RequestMapping("save.do")
    public String save(Product product) throws Exception{

        // 添加产品
        productService.save(product);

        return "redirect:findAll.do";
    }
}
